package com.ece.devirtualizus.Dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.util.Log;

import com.ece.devirtualizus.Activities.GestionAmisActivity;
import com.ece.devirtualizus.Entities.StepEntity;
import com.ece.devirtualizus.ModelDto.Modules;
import com.ece.devirtualizus.ModelDto.ResultatSessionDto;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class ReleveDao implements IReleveDao {

	static final int limitReleveSession = 10;

	private GestionAmisActivity activity;

	public ReleveDao() {
		// TODO Auto-generated constructor stub
	}

	public ReleveDao(GestionAmisActivity activity) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
	}

	@Override
	public boolean storeResultsInBdd(String idUser,
			ResultatSessionDto resSession, Modules module) {
		// TODO Auto-generated method stub

		String releveId = this.getModuleKey(module);
		if (releveId != "") {
			// ParseObject typeReleve = new ParseObject("TypeReleve");
			try {
				ParseObject actionReleve = new ParseObject("ActionReleve");
				actionReleve.put("nbRepetitions", resSession.getCompteur());
				actionReleve.put("pointsSession", resSession.getPointAcquis());
				actionReleve.put("dateBeginSession",
						resSession.getDateBeginSession());
				actionReleve.put("dateEndSession",
						resSession.getDateEndSession());
				actionReleve.put("idType", releveId);
				actionReleve.put("idUser", idUser);
				actionReleve.save();
				return true;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("storeResultsInBdd.releveId", "" + e.getMessage());
				return false;
			}
		} else {
			Log.i("storeResultsInBdd", "Module Type Id not found");
			return false;
		}

	}

	private List<ParseObject> getAllReleveByModuleParse(String idUser,
			String module) {
		Log.i("getAllReleveParseByModule", "idUser : " + idUser);

		List<ParseObject> listResSessParse = new ArrayList<ParseObject>();

		if (module != "") {
			ParseQuery<ParseObject> queryTypeReleve = ParseQuery
					.getQuery("ActionReleve");
			queryTypeReleve.whereEqualTo("idUser", idUser);
			queryTypeReleve.whereEqualTo("idType", module);

			try {
				listResSessParse = queryTypeReleve.find();
				Log.i("getAllReleveParseByModule", "listResSessParse size : "
						+ listResSessParse.size());
				return listResSessParse;
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return listResSessParse;
			}

		} else {
			return listResSessParse;
		}

	}

	private List<ParseObject> getAllReleveByDateParse(String idUser,
			Date dateBeginSearch, Date dateEndSearch) {
		List<ParseObject> listResSessParse = new ArrayList<ParseObject>();

		ParseQuery<ParseObject> queryTypeReleve = ParseQuery
				.getQuery("ActionReleve");
		queryTypeReleve.whereEqualTo("idUser", idUser);
		queryTypeReleve.whereGreaterThanOrEqualTo("dateBeginSession",
				dateBeginSearch);
		queryTypeReleve.whereLessThanOrEqualTo("dateBeginSession",
				dateEndSearch);

		try {
			listResSessParse = queryTypeReleve.find();
			return listResSessParse;
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return listResSessParse;
		}

	}

	private List<ParseObject> getAllReleveParse(String idUser) {
		List<ParseObject> listResSessParse = new ArrayList<ParseObject>();

		ParseQuery<ParseObject> queryTypeReleve = ParseQuery
				.getQuery("ActionReleve");
		queryTypeReleve.whereEqualTo("idUser", idUser);

		try {
			listResSessParse = queryTypeReleve.find();
			return listResSessParse;
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return listResSessParse;
		}
	}

	private List<ResultatSessionDto> convertParseReleveToReleveDtoParse(
			List<ParseObject> allReleveParse) {
		List<ResultatSessionDto> allResultSess = new ArrayList<ResultatSessionDto>();

		for (ParseObject obj : allReleveParse) {
			ResultatSessionDto resSess = new ResultatSessionDto();

			resSess.setCompteur(obj.getInt("nbRepetitions"));
			resSess.setDateBeginSession(obj.getDate("dateBeginSession"));
			resSess.setDateEndSession(obj.getDate("dateEndSession"));
			resSess.setPointAcquis(obj.getInt("pointsSession"));
			allResultSess.add(resSess);
		}
		Log.i("convertParseReleveToReleveDto", "allResultSess size : "
				+ allResultSess.size());

		return allResultSess;
	}

	private List<ParseObject> getAllReleveByModuleByDateParse(String idUser,
			String moduleKey, Date dateBeginSearch, Date dateEndSearch) {
		List<ParseObject> listResSessParse = new ArrayList<ParseObject>();

		if (moduleKey != "") {
			ParseQuery<ParseObject> queryTypeReleve = ParseQuery
					.getQuery("ActionReleve");
			queryTypeReleve.whereEqualTo("idUser", idUser);
			queryTypeReleve.whereEqualTo("idType", moduleKey);
			queryTypeReleve.whereGreaterThanOrEqualTo("dateBeginSession",
					dateBeginSearch);
			queryTypeReleve.whereLessThanOrEqualTo("dateBeginSession",
					dateEndSearch);
			queryTypeReleve.orderByAscending("dateBeginSession");

			try {
				listResSessParse = queryTypeReleve.find();
				Log.i("getAllReleveByModuleByDateParse",
						"listResSessParse size : " + listResSessParse.size());
				return listResSessParse;
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return listResSessParse;
			}
		} else {
			return listResSessParse;
		}
	}

	private String getModuleKey(Modules module) {

		ParseObject moduleTypeParse = new ParseObject("TypeReleve");
		String moduleId = "";

		ParseQuery<ParseObject> queryTypeReleveId = ParseQuery
				.getQuery("TypeReleve");
		queryTypeReleveId.whereEqualTo("libelleReleve", module.toString());

		try {
			moduleTypeParse = queryTypeReleveId.getFirst();
			moduleId = moduleTypeParse.getObjectId();
			Log.i("getModuleKey", "module id : " + moduleId);
			return moduleId;
		}

		catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return moduleId;
		}

	}

	public List<ResultatSessionDto> getAllReleveDtoByModule(String idUser,
			Modules module) {
		String moduleKey = "";
		List<ResultatSessionDto> resSess = new ArrayList<ResultatSessionDto>();
		List<ParseObject> resParseSess = new ArrayList<ParseObject>();

		moduleKey = getModuleKey(module);
		resParseSess = getAllReleveByModuleParse(idUser, moduleKey);
		resSess = convertParseReleveToReleveDtoParse(resParseSess);
		return resSess;
	}

	public List<ResultatSessionDto> getAllReleveDtoByDate(String idUser,
			Date dateBeginsession, Date dateEndSession) {
		List<ResultatSessionDto> resSess = new ArrayList<ResultatSessionDto>();
		List<ParseObject> resParseSess = new ArrayList<ParseObject>();

		resParseSess = getAllReleveByDateParse(idUser, dateBeginsession,
				dateEndSession);
		resSess = convertParseReleveToReleveDtoParse(resParseSess);

		return resSess;
	}

	public List<ResultatSessionDto> getAllReleveDto(String idUser) {
		List<ResultatSessionDto> resSess = new ArrayList<ResultatSessionDto>();
		List<ParseObject> resParseSess = new ArrayList<ParseObject>();

		resParseSess = getAllReleveParse(idUser);
		resSess = convertParseReleveToReleveDtoParse(resParseSess);

		return resSess;
	}

	public List<ResultatSessionDto> getAllReleveDtoByModuleByDate(
			String idUser, Modules module, Date dateBeginsession,
			Date dateEndSession) {
		String moduleKey = "";
		List<ResultatSessionDto> resSess = new ArrayList<ResultatSessionDto>();
		List<ParseObject> resParseSess = new ArrayList<ParseObject>();

		moduleKey = getModuleKey(module);
		resParseSess = getAllReleveByModuleByDateParse(idUser, moduleKey,
				dateBeginsession, dateEndSession);
		resSess = convertParseReleveToReleveDtoParse(resParseSess);

		return resSess;
	}

	@Override
	public int updateStepCount(String idUser, StepEntity stepEntity, int cpt) {
		// GET ID STEP MODULE
		ParseObject stepModuleIdParse = new ParseObject("TypeReleve");
		String stepModuleId = "";
		ParseQuery<ParseObject> queryTypeReleveId = ParseQuery
				.getQuery("TypeReleve");
		queryTypeReleveId.whereEqualTo("libelleReleve",
				Modules.STEP_COUNTER.toString());

		try {
			stepModuleIdParse = queryTypeReleveId.getFirst();
			stepModuleId = stepModuleIdParse.getObjectId();
			Log.i("------ getModuleKey ------", "module id : " + stepModuleId);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// GET STEP ACTION REVELE VIA STEP ID MODULE
		List<ParseObject> stepSessionParse = new ArrayList<ParseObject>();
		ParseQuery<ParseObject> queryStepSession = ParseQuery
				.getQuery("ActionReleve");
		queryStepSession.whereEqualTo("idType", stepModuleId);
		queryStepSession.whereEqualTo("idUser", idUser);

		try {
			stepSessionParse = queryStepSession.find();

			// IF EMPTY -> INSERT
			if (stepSessionParse.size() == 0) {
				ResultatSessionDto resSession = new ResultatSessionDto();
				resSession.setModule(Modules.STEP_COUNTER);
				resSession.setCompteur(stepEntity.getCount());
				resSession
						.setDateBeginSession(stepEntity.getDateBeginSession());
				resSession.setDateEndSession(stepEntity.getDateEndSession());
				// FIELD TO REMOVE FROM PARSE BDD
				resSession.setPointAcquis(0);
				this.storeResultsInBdd(idUser, resSession, Modules.STEP_COUNTER);
			} else {
				// stepSessionParse = queryStepSession.f;
				// IF DATE SESSION != DATE UPDATE -> INSERT
				Date dateBeginSession = stepSessionParse.get(cpt).getDate(
						"dateBeginSession");
				Log.d("----- DATE-----", dateBeginSession.toString() + " | "
						+ stepEntity.getDateBeginSession().toString());
				if (dateBeginSession.getDay() == stepEntity
						.getDateBeginSession().getDay()
						&& dateBeginSession.getMonth() == stepEntity
								.getDateBeginSession().getMonth()
						&& dateBeginSession.getYear() == stepEntity
								.getDateBeginSession().getYear()) {
					// UPDATE LINE
					int stepCount = stepSessionParse.get(cpt).getInt(
							"nbRepetitions");
					// SUM OLD STEP COUNT WITH THE NEW ONE
					Log.d("----- COUNT STEP ENTITY ET STEP RECUP-----",
							Integer.toString(stepCount) + " | "
									+ Integer.toString(stepEntity.getCount()));
					stepEntity.setCount(stepEntity.getCount() + stepCount);
					stepSessionParse.get(cpt).put("nbRepetitions",
							stepEntity.getCount());
					Log.d("----- COUNT STEP SAVE -----",
							Integer.toString(stepEntity.getCount())
									+ " | "
									+ stepEntity.getDateBeginSession()
											.toString());
					stepSessionParse.get(cpt).saveInBackground();
					Log.d("---- updateStepCount DATE ----", "YO");
				} else {
					ResultatSessionDto resSession = new ResultatSessionDto();
					resSession.setModule(Modules.STEP_COUNTER);
					resSession.setCompteur(stepEntity.getCount());
					resSession.setDateBeginSession(stepEntity
							.getDateBeginSession());
					resSession
							.setDateEndSession(stepEntity.getDateEndSession());
					// FIELD TO REMOVE FROM PARSE BDD
					resSession.setPointAcquis(0);
					this.storeResultsInBdd(idUser, resSession,
							Modules.STEP_COUNTER);
					cpt++;
					Log.d("---- updateStepCount DATE ----", "ELSE DE DATE");
				}
			}
			return cpt;
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return cpt;
		}
	}
}
